package com.ibt.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ibt.entity.Employee;
import com.ibt.util.ApplicationContext;

public class EmployeeDAO {
	static Transaction tx;

	public static int insert(Employee b) throws Exception {

		Session session = startOperations();
		session.saveOrUpdate(b);
		session.getTransaction().commit();
		endOperations(session);
		return 0;
	}

	public static int update(Employee b) throws Exception {
		Session session = startOperations();
		Query qry = session
				.createQuery("update Employee set current_points=:points where business_id=:busid and customer_id=:custid");
		//qry.setInteger("points", b.getCurrentPoints());
		////qry.setInteger("busid", b.getBusiness().getBusinessId());
		//qry.setInteger("custid", b.getCustomer().getCustomerId());
		Object ob = qry.executeUpdate();
		session.getTransaction().commit();
		endOperations(session);
		return 0;
	}

	public static int delete(Employee b) throws Exception {
		Session session = startOperations();
		session.delete(b);
		// tx.commit();
		endOperations(session);
		return 0;
	}

	private static void endOperations(Session session) {
		session.close();
		session = null;
	}

	private static Session startOperations() throws Exception {
		Session session = ApplicationContext.getHiberSession().openSession();
		tx = session.beginTransaction();
		return session;
	}

	public static List<Employee> findAll(int busid) throws Exception {
		List<Employee> custObjs = new ArrayList<Employee>();
		List objs = null;
		Session session = startOperations();
		Query query = session.createQuery("from Employee where role='developer' and business_id=:busid ");
		query.setInteger("busid",busid);
		objs = query.list();
		for (Object o : objs) {
			custObjs.add((Employee) o);
		}
		endOperations(session);
		return custObjs;
	}

	public static Employee findById(int i) throws Exception {
		Session session = startOperations();
		Employee c = (Employee) session.load(
				Employee.class, i);
		endOperations(session);
		return c;
	}

	public static Object getByCustBusId(int id, int i) throws Exception {
		Session session = startOperations();
		System.out.println("id in dao " + id);
		Query qry = session
				.createQuery("from Employee where customer_id=:id and business_id=:bid");
		qry.setInteger("id", id);
		qry.setInteger("bid", i);
		Object obj = qry.uniqueResult();
		System.out.println("obj is " + obj);

		endOperations(session);
		return obj;

	}

	public static Employee getBusinessCustMap(String userid)
			throws Exception {
		Session session = startOperations();
		System.out.println("id in dao " + userid);
		Query qry = session
				.createQuery("from Employee where email=:userid");
		qry.setString("userid", userid);
		Object obj = qry.uniqueResult();
		Employee emp=(Employee)obj;
		return emp;

	}

	public static List<Employee> findByBusId(int bsid) throws Exception {

		Session session = startOperations();
		Query qry = session
				.createQuery("from Employee where business_id=:id");
		qry.setInteger("id", bsid);
		List<Employee> listBC = qry.list();
		endOperations(session);
		return listBC;
	}


}
